/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.OrderManager;
import core.ScreenEngine;
import core.Settings;
import datamodel.Order;
import view.commands.BackCommand;
import view.commands.EmptyCommand;

/**
 *
 * @author Kvest
 */
public class OrderForm extends BasicForm
{
    private static final int FAST_GET_COMMAND_ID = 1;
    private static final int TIME_COMMAND_ID = 2;
    private static final int ACCEPT_COMMAND_ID = 3;
    
    private Order order;
    
    private TextArea text;
    
    private int fastGetTime;
    
    private BackCommand backCommand;
    private EmptyCommand emptyCommand;
    private Command fastGetOrder;
    private Command timeCommand;
    private Command acceptCommand;
    
    private TimeSelectForm timeSelectForm;
    
    public OrderForm(String title)
    {
        super(title);
        
        init();
    }

    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        setCyclicFocus(false);
        
        text = new TextArea("", 1, 30);
        text.setEditable(false);
        text.setGrowByContent(true);
        addComponent(text);
        
        backCommand = new BackCommand();
        emptyCommand = new EmptyCommand();
        fastGetTime = 10;
        fastGetOrder = new Command(Integer.toString(fastGetTime), FAST_GET_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                int result = IMAPMIDlet.getMidlet().getOrderManager().getOrderRequest(order, fastGetTime);
                
                //Возвращаемся на главную форму
                ScreenEngine.getEngine().popTo(0);
                
                if (result == OrderManager.GO_IS_DELETED)
                {
                    //Выводим сообщение что заказ устарел
                    ScreenEngine.getEngine().showMessageBox("message", (new Label("order_is_old")).getText(), 5000);
                }
                
                evt.consume();
            }
        };
        timeCommand = new Command("time", TIME_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                showTimeSelectForm();

                evt.consume();
            }
        };
        acceptCommand = new Command("accept", ACCEPT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Подписываемся на заказ
                IMAPMIDlet.getMidlet().getOrderManager().signPrelimOrder(order);
                
                //Закрываем форму
                ScreenEngine.getEngine().pop(true);
                        
                evt.consume();
            }
        };
    }
    
    public void setOrder(Order order) 
    {
        this.order = order;
        
        //Меняем текст заказа
        if (order.isPreliminary())
        {
            text.setText(order.getOrderPrelimFullDesc());
        }
        else
        {
            text.setText(order.getOrderFullDesc());
        }
        
        //Если форма показана - обновляем ее
        if (isVisible())
        {
            setCommands();
            repaint();
        }
    }
    
    public void show()
    {
        setCommands();
        
        super.show();
    }
    
    private void setCommands()
    {
        removeAllCommands();
        
        if (order.getStatus() == Order.STATE_NEW)
        {
            //Это новый заказ
            if (order.isPreliminary())
            {
                addCommand(emptyCommand);
                addCommand(backCommand);
                addCommand(acceptCommand);
            }
            else
            {
                //Настраиваем время на центр. кнопке
                if (order.isEfirOrder())
                {
                    fastGetTime = Settings.getSettings().getEfirTime();
                }
                else
                {
                    fastGetTime = Settings.getSettings().getDispTime();
                }
                fastGetOrder.setCommandName(Integer.toString(fastGetTime));
                addCommand(fastGetOrder);
                addCommand(backCommand);
                addCommand(timeCommand);
            }
        }
        else
        {
            //Заказ не новый
            addCommand(emptyCommand);
            addCommand(backCommand);
        }
    }
    
    private void showTimeSelectForm()
    {
        if (timeSelectForm == null)
        {
            timeSelectForm = new TimeSelectForm("arrive", new TimeSelectForm.TimeSelected() 
            {
                public void timeSelected(int time) 
                {
                    int result = IMAPMIDlet.getMidlet().getOrderManager().getOrderRequest(order, time);
                    
                    //Возвращаемся на главную форму
                    ScreenEngine.getEngine().popTo(0);
                    
                    if (result == OrderManager.GO_IS_DELETED)
                    {
                        //Выводим сообщение что заказ устарел
                        ScreenEngine.getEngine().showMessageBox("message", (new Label("order_is_old")).getText(), 5000);
                    }
                }
            });
        }
        
        ScreenEngine.getEngine().push(timeSelectForm);
                
    }
}
